<template>
  <div class="login-container">
   <el-card header="请先登录" class="login-card">
    <el-form class="login-form" @submit.native.prevent="login">
      <el-form-item label="用户名">
        <el-input v-model="model.username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input type="password" v-model="model.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" native-type="submit">登录</el-button>
      </el-form-item>
    </el-form>
   </el-card>
  </div>
</template>


<script>
  export default {
    data(){
      return {
        model: {}
      }
    },
    methods: {
      async login(){
        // console.log(this.model)
        //接口请求, 获得的结果是一个token
        const res = await this.$http.post('login', this.model);
        // console.log(res.data)
        //将token保存在系统中,下次打开也存在
        localStorage.token = res.data;
      //  设置session，下次打开，没有
      //   sessionStorage.token = res.data
        this.$router.push('/');
        this.$message({
          type: 'success',
          message: '登陆成功'
        })
      }
    }
  }
</script>
<style>
  .login-card{
    width: 28%;
    margin: 6rem auto;
  }
</style>

